class Solution
{
public:
    string compressedString(string word)
    {
        char last = '\0';
        int lastCount = 0;
        string result;
        for (char c : word)
        {
            if (c == last)
            {
                ++lastCount;
                if (lastCount == 9)
                {
                    lastCount = 0;
                    result.push_back('9');
                    result.push_back(last);
                }
            }
            else
            {
                if (lastCount > 0)
                {
                    result.push_back(lastCount + '0');
                    result.push_back(last);
                }
                last = c;
                lastCount = 1;
            }
        }
        if (lastCount > 0)
        {
            result.push_back(lastCount + '0');
            result.push_back(last);
        }
        return result;
    }
};